package com.exam.aopsolution;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Order(2) //---------------------------------定义优先级-------------------------
public class RetryAspect {
    @Pointcut("@annotation(com.exam.aopsolution.Retry)")
    public void pointcut() { }
    @Around("pointcut()")
    public Object retry(ProceedingJoinPoint pjp) throws Throwable {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Retry retry = signature.getMethod().getDeclaredAnnotation(Retry.class);
    int maxAttempts = retry.maxAttempts();
    int attempts = 1;
        while (attempts < maxAttempts) {
             try {
                 return pjp.proceed();
                 } catch (Throwable e) {
                 attempts++;
                 }
             }
         return pjp.proceed();
         }
 }